const logon = document.querySelector('.banner> .login >ol > li >.logon')
const nameInp = document.querySelector('.banner> .login >ol > li >.logon>p>.username')
const pwdInp = document.querySelector('.banner> .login >ol > li >.logon>p>.password')
const btn = document.querySelector('.banner> .login >ol > li >.logon > button')
const error = document.querySelector('.banner> .login >ol > li >.logon>.error')

logon.addEventListener('submit',async e =>{
    e = e || window.event
    try{ e.preventDefault()}catch(err){e.returnValue = false}
    const name = nameInp.value
    const pwd = pwdInp.value
    if(!name || !pwd ) return window.alert('请完整输入用户名和密码')
  const res = await pAjax({
    url : '../../server/login.php',
    method : 'POST',
    dataType : 'json',
    data :  `username=${name}&password=${pwd}`,
    
})
console.log(res);
if(res.code === 0 ){
    error.classList.add('active')
    return
}
   window.localStorage.setItem('login',1)
   window.localStorage.setItem('nickname',res.info[0].nickname)
   window.localStorage.setItem('id',res.info[0].Id)
   const url = window.localStorage.getItem('url')
   window.localStorage.removeItem('url')
   window.location.href = `./${ url ? url : 'index' }.html`
})

const register = document.querySelector('.banner> .login >ol > li >.register')
const nameInp1 = document.querySelector('.banner> .login >ol > li >.register>p>.name')
const newnameInp = document.querySelector('.banner> .login >ol > li >.register>p>.newname')
const pswInp1 = document.querySelector('.banner> .login >ol > li >.register>p>.password1')
const newpswInp = document.querySelector('.banner> .login >ol > li >.register>p>.newpassword')
const arror = document.querySelector('.banner> .login >ol > li >.register>.arror')
const crror = document.querySelector('.banner> .login >ol > li >.register>.crror')
const empty = document.querySelector('.banner> .login >ol > li >.register>.empty')

register.addEventListener('submit',async e => {
    e = e || window.event
    try { e.preventDefault() } catch (err) { e.returnValue = false }
    const name1 = nameInp1.value
    const psw1 = pswInp1.value
    const nickname = newnameInp.value
    const rpassword = newpswInp.value

    if (!name1 || !psw1 || !nickname || !rpassword) return window.alert('请输入正确的用户名和密码')
    if( psw1 !== rpassword)  return window.alert('重复密码必须和密码一致')
    const res = await pAjax({
      url : '../../server/register.php',
      method : 'POST',
      dataType : 'json',
      data :  `username=${name1}&password=${psw1}&nickname=${nickname}&rpassword=${rpassword}`,
  })
    console.log(res)
    if (res.code === 2) {
        arror.classList.add('active')
        return 
    }
    crror.classList.add('active')
    empty.classList.add('active')
  })

